import { resolve } from 'path'
import { Module } from '@nestjs/common'
import { TypeOrmModule } from '@nestjs/typeorm'
import { Connection } from 'typeorm'
import { ConfigModule, ConfigService } from 'nestjs-config'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { UserModule } from './modules/user/user.module'
import { ArticleModule } from './modules/article/article.module'
import { CommentModule } from './modules/comment/comment.module'
import { EmailModule } from './modules/email/email.module'
import { MailerModule } from '@nestjs-modules/mailer'
import { RedisModule } from 'nestjs-redis'
import { AuthModule } from './modules/auth/auth.module';
import options from './config/redis'

@Module({
  imports: [
    ConfigModule.load(resolve(__dirname, 'config', '**/!(*.d).{ts,js}')),
    TypeOrmModule.forRootAsync({
      useFactory: (config: ConfigService) => config.get('database'),
      inject: [ConfigService]
    }),
    MailerModule.forRootAsync({
      useFactory: (config: ConfigService) => config.get('email'),
      inject: [ConfigService]
    }),
    AuthModule,
    RedisModule.register(options),
    UserModule,
    ArticleModule,
    CommentModule,
    EmailModule
  ],
  controllers: [AppController],
  providers: [AppService]
})
export class AppModule {
  constructor(private readonly connection: Connection) {}
}
